package com.xian.controller;

import com.xian.pojo.Product;
import com.xian.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.security.DenyAll;
import javax.annotation.security.PermitAll;
import javax.annotation.security.RolesAllowed;
import java.util.List;

@Controller
@RequestMapping("/product")
public class ProductController {
//    调service层
    @Autowired
    @Qualifier("productServiceImpl")
    private ProductService productService;
    //查询全部产品
    @RequestMapping("/findAll.do")
    @RolesAllowed("ADMIN")
    public ModelAndView findAll(ModelAndView mv){
        List<Product> products = productService.queryAllProduct();
        System.out.println(products);
        mv.addObject("products",products);
        List<Product> products1 = (List<Product>) mv.getModelMap().getAttribute("products");
        System.out.println(products1);
        mv.setViewName("product-list");
        return mv;
    }

    // 添加产品
    @RequestMapping("/save.do")
    public String save(Product product){
        int i = productService.saveProduct(product);
        return "redirect:findAll.do";
    }
}
